//
//  LGSoundRecordingController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/7/14.
//  录音测试

#import "LGSoundRecordingController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>


@interface LGSoundRecordingController ()
/**   */
@property (nonatomic , strong)AVAudioSession *session;
/**   */
@property (nonatomic , strong)AVAudioRecorder *recorder;

@end

@implementation LGSoundRecordingController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"录音测试";
    self.view.backgroundColor = [UIColor colorWithString:@"#F2F3F7"];
    
    UIButton *startBtn = [UIButton buttonWithTitle:@"开始录音" topMargin:viewPix(100)];
    [startBtn addTarget:self action:@selector(startRecordAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:startBtn];
    
    UIButton *stopBtn = [UIButton buttonWithTitle:@"停止录音" topMargin:viewPix(200)];
    [stopBtn addTarget:self action:@selector(stopRecordAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:stopBtn];
    
    UIButton *playBtn = [UIButton buttonWithTitle:@"回访录音" topMargin:viewPix(300)];
    [playBtn addTarget:self action:@selector(playRecordAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:playBtn];
    
}

//开始录音
-(void)startRecordAction{
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]stringByAppendingPathComponent:@"record_file"];
    // 判断文件夹是否存在
    if(![[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil] count]){
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *filePath = [path stringByAppendingPathComponent:@"/record2.m4a"];
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    [session setActive:YES error:nil];
    self.session = session;
    

    NSDictionary *recordSetting = [[NSDictionary alloc]
        initWithObjectsAndKeys:
            /**
             * 采样率：8000/11025/22050/44100/96000（该参数影响音频的质量）
             * Sampling rate: 8000/11025/22050/44100/96000 (this parameter affects the audio
             * quality)
             */
                [NSNumber numberWithFloat:16000.0], AVSampleRateKey,
                /**
                 * 音频格式
                 * Audio format
                 */
                [NSNumber numberWithInt:kAudioFormatMPEG4AAC], AVFormatIDKey,
                /**
                 * 采样位数：  8、16、24、32 默认为 16
                 * Sampling bits: 8, 16, 24, 32, default is 16
                 */
                [NSNumber numberWithInt:16], AVLinearPCMBitDepthKey,
                /**
                 * 音频通道数 1 或 2
                 * Number of audio channels 1 or 2
                 */
                [NSNumber numberWithInt:1], AVNumberOfChannelsKey,
                /**
                 * 录音质量
                 * Recording quality
                 */
                [NSNumber numberWithInt:AVAudioQualityHigh], AVEncoderAudioQualityKey, nil];

//    [self createRecordedFilePath];

    NSURL *url = [NSURL fileURLWithPath:filePath];
    self.recorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:nil];
    self.recorder.meteringEnabled = YES;
    [self.recorder prepareToRecord];
    [self.recorder record];
    [self.recorder updateMeters];
}

//停止录音
-(void)stopRecordAction{
    [self.recorder stop];
//    [self.session setActive:NO error:nil];
}

//回访录音
-(void)playRecordAction{
    [self.session setCategory:AVAudioSessionCategorySoloAmbient error:nil];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
